import random

from PyPixelCore.PyPixelController import PyPixelController
from PyPixelCore.Setting import *
from PyPixelCore.block.Block import Block
from PyPixelCore.block.DynamicImageBlock import DynamicImageBlock
from PyPixelCore.block.TextBlock import TextBlock
from PyPixelCore.block.TextButton import TextButton


def baseAnimal(self: Block):
    if self.rect.width > 80:
        self.state = 1
    elif self.rect.width < 20:
        self.state = 0
    if self.state == 0:
        self.rect.width += 1
        self.bk_color = (self.bk_color[0] - 3, self.bk_color[1] - 3, self.bk_color[2] - 3)
    else:
        self.rect.width -= 1
        self.bk_color = (self.bk_color[0] + 3, self.bk_color[1] + 3, self.bk_color[2] + 3)
    self.is_dirty = True


def main():
    gc = PyPixelController()
    # 普通块
    base_block = Block(100, 50, Color_type.BLACK, point_x=50, point_y=100, collision=False, is_alpha=False)
    base_block.set_frame_animation(baseAnimal)

    # 信息块
    message_block = TextBlock("测试\n不支持换行", 200, 200)
    message_block.set_color_no_None(font_color=Color_type.WHITE, bk_color=Color_type.GREY)

    message_random = TextBlock("你改变了位置", 100, 100)

    # 按钮
    do_random_button = TextButton("按钮测试", point_x=400, point_y=100)

    def test_remove(self):  # 基础按钮的点击事件,让某个方块在屏幕上随机移动
        message_random.set_font_color((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        message_random.rect.x = random.randint(0, window_width - message_random.rect.width)
        message_random.rect.y = random.randint(0, window_height - message_random.rect.height)

    do_random_button.set_click_down_func(test_remove)

    # 静态图片块
    static_block = Block(point_x=500, point_y=0, width=100, height=100)
    static_block.load_image("./recourse/test.png", size_lock=True)

    # 动态图片块
    dynamic_block = DynamicImageBlock()
    dynamic_block.add_image_list_from_images("./recourse/horse.png", ["down", "left", "right", "up"], 6, 4, False)
    dynamic_block.set_video_state("down")
    dynamic_block.set_animation_interval(8)

    gc.add_block(base_block, message_block, do_random_button, static_block, dynamic_block, message_random)
    gc.run()


if __name__ == '__main__':
    main()
